
#include <QtGui>
#include <QPair>


#include "FractalRect.h"


static void paintArea( QPainter *p , const QRectF rect , QColor col )
{
    p->save();
    p->setBrush(col);
    p->setPen(Qt::NoPen);
    p->drawRect(rect);
    p->restore();
}

#define _SLIDERBGCOLOR_ \
             QColor("#ebe9ed")
#define _EDITORBGCOLOR_ \
             QColor("#999999")
#define _SHADOWCOLOR_ \
             QColor("#7b6868")



static const int IMG_REGULAR_WI = 1500;
static const int IMG_REGULAR_HI = 1500;


static const qreal PAGEFAKTOR = 1.4;
static const qreal SCALING_STEEP = 0.05555555555;
static const qreal SLIDERMARGIN_TICK = 20.0;
static const qreal SLIDERSPACER = 2.5;
static const qreal SLIDERCURSORWI = 6.0;
static const qreal PAPERSPACE = 5;
static const int MaximumPages = 99;
static const qreal BorderShadow = 5.2;
static const int DefaultFonzSize = 10;
static const qreal SLIDERMARGIN_TICK_LEFT = SLIDERMARGIN_TICK + SLIDERSPACER;
static const qreal SLIDERMARGIN_TICK_TOTAL = SLIDERMARGIN_TICK + SLIDERSPACER + SLIDERSPACER;
typedef QPair<qreal,qreal> bodyMargins;


typedef QPair<qreal,qreal> bodyMargins;

      


class EditImage : public QAbstractScrollArea
{
    Q_OBJECT

public:
     EditImage( QWidget *parent = 0 );
     inline int xOffset() const { return horizontalScrollBar()->value(); }
     inline int yOffset() const { return verticalScrollBar()->value(); }
protected:
     void paintEvent( QPaintEvent *Event );
     void adjustScrollbars();
     void wheelEvent (QWheelEvent * event);
     void resizeEvent(QResizeEvent *e);
     void keyPressEvent ( QKeyEvent * e );
private:
    
    QRectF page;
    qreal mesure; /* fix faktor cm*/
    QTransform mcurrent;
    QSize workArea;  /* mobile size from zoom */
    qreal scaleFaktor;
    QRectF slider_Horrizzontal_Top;
    QRectF slider_Vertical_Left;
    FractalRect *fraction_img;

signals:
public slots:
    void fitToLarge();
    void fitToNormal();
    void setZoom( const qreal value );
    void zoomIn();
    void zoomOut();
    
};
 
/**/
EditImage::EditImage( QWidget *parent)
  : QAbstractScrollArea(0),scaleFaktor(0.2)
{

  fraction_img = new FractalRect(5000,5000);
  adjustScrollbars();
  setAcceptDrops ( true );
  resize(500,500);
  
}

void EditImage::paintEvent( QPaintEvent *Event )
{
    
    QPainter *p = new QPainter( viewport() );
    p->setRenderHint(QPainter::Antialiasing, true);
    paintArea(p,Event->rect(),_EDITORBGCOLOR_);
    const int y = yOffset();
    const int x = xOffset();
    p->translate(-x,-y);
    p->setWorldTransform(mcurrent,true);
    /////////p->setViewTransformEnabled ( true );
    const QRect viRect(+x,+y,Event->rect().width(),Event->rect().height());
    fraction_img->paintImage(p,viRect); /////////// p->
    p->end();
    /////QAbstractScrollArea::paintEvent(Event);
}


void EditImage::resizeEvent(QResizeEvent *e)
{
    adjustScrollbars();
}

void EditImage::keyPressEvent ( QKeyEvent * e )  
{
    if ( e == QKeySequence::ZoomIn) {
        zoomIn();
    }
    if ( e == QKeySequence::ZoomOut) {
        zoomOut();
    }
}

void EditImage::zoomIn()
{
    setZoom(mcurrent.m11() + SCALING_STEEP);
}

void EditImage::zoomOut()
{
    setZoom(mcurrent.m11() - SCALING_STEEP);
}

void EditImage::setZoom( const qreal value )
{
    if (value < 0.02 || value > 22) {
     return;
    } else {
      qDebug() << "### setZoom " << value; 
      scaleFaktor = value;
      adjustScrollbars();
      viewport()->update();
    }
}

void EditImage::wheelEvent (QWheelEvent * event)
{
  qreal docksscale = scaleFaktor;
  if (event->delta() > 1) {
      zoomOut();
  } else {
      zoomIn();
  }
}


void EditImage::fitToLarge()
{
    adjustScrollbars();
    const QRectF viewAreaRect(0,0,viewport()->width(),viewport()->height());
    const qreal maxavaiable = viewAreaRect.width() - SLIDERMARGIN_TICK_TOTAL;
    setZoom(maxavaiable / page.width());
}

void EditImage::fitToNormal()
{
    setZoom(1.0);
}


/*

void EditImage::contextMenuEvent(QContextMenuEvent *event)
{
        QMenu *menu = new QMenu(this);
        menu->addAction(tr("Swap page format"),this, SLOT(triggerFormat()));
        menu->addAction(tr("Fit to Window"),this, SLOT(fitToLarge()));
        menu->addAction(tr("Reset zoom to 1:1"),this, SLOT(fitToNormal()));
        menu->addAction(tr("Zoom in CTRL++"),this, SLOT(zoomIn()));
        menu->addAction(tr("Zoom out CTRL+-"),this, SLOT(zoomOut()));
        menu->exec(event->globalPos());
        delete menu;
}
*/



void EditImage::adjustScrollbars()
{
    QFont f = font();
    f.setPointSize(11);
    f.setBold ( true );
    setFont(f);

    /* portrait - landscape */
    qreal lmax = fraction_img->width();
    qreal lmin = fraction_img->height();
    page = QRectF(0,0,lmax,lmin);
    workArea = QSize(page.width() * scaleFaktor ,page.height() * scaleFaktor);
    bool stayinwi =  workArea.width() < viewport()->width() ? true : false;
    bool stayinhi =  workArea.height() < viewport()->height() ? true : false;
    
    mcurrent  = QTransform(scaleFaktor,0.,0.,scaleFaktor,0,0); ////// ultimo da sopra penultimo da sinistra
    QSize viewPanelSize = viewport()->size();
    setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
    setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
    verticalScrollBar()->setPageStep(workArea.height());  /* zoom state */
    horizontalScrollBar()->setPageStep(workArea.width()); /* zoom state */
    verticalScrollBar()->setRange(0, workArea.height()  - viewPanelSize.height() + SLIDERMARGIN_TICK_TOTAL * 3);
    horizontalScrollBar()->setRange(0, ( page.width() * scaleFaktor) - viewPanelSize.width()  + SLIDERMARGIN_TICK_TOTAL * 3);
}

  


int main(int argc, char **argv)
{
   QApplication a(argc, argv);
   EditImage w;
   w.show();
   return a.exec();
}

#include "main.moc" 






